package jrmob.yonyou.com.commonlib.app_config_tool.tool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Marvin on 2016/8/18.
 */

public class ChangeHeadImgObserverUtilTD {
    /**
     * 存放注册的观察者
     */
    private static List<ChangeHeadImgObserverTD> observers = new ArrayList<ChangeHeadImgObserverTD>();
    /**
     * 单例
     */
    private static ChangeHeadImgObserverUtilTD instance = null;

    private ChangeHeadImgObserverUtilTD() {
        super();
    }

    /**
     * 获取实例
     * @return
     */
    public static ChangeHeadImgObserverUtilTD getInstance() {
        if (null == instance) {
            synchronized (ChangeHeadImgObserverUtilTD.class) {
                if (null == instance) {
                    instance = new ChangeHeadImgObserverUtilTD();
                }
            }
        }
        return instance;
    }

    /**
     * 注册观察者
     * @param observer 观察者对象
     */
    public void registerChangeHeadImgObserver(ChangeHeadImgObserverTD observer) {
        observers.add(observer);
    }

    /**
     * 反注册观察者
     * @param observer 观察者对象
     */
    public void unRegisterChangeHeadImgObserver(ChangeHeadImgObserverTD observer) {
        observers.remove(observer);
    }

    /**
     * 数据变化通知所有观察者
     */
    public void notifyAllObserver(String url) {
        if (null != observers && observers.size() > 0) {
            for (ChangeHeadImgObserverTD observer : observers) {
                observer.update(url);
            }
        }
    }
}
